﻿using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Data;
using LIB.Core;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class wsinh_vien
{
    [WebGet]
    [OperationContract]
    public string Fs_TUYEN_SINH_LKE(string id_phieu)
    {
        try
        {
            DataSet b_ds = c_nhapdiem.Fds_TUYEN_SINH_LKE(id_phieu);

            return "";
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [WebGet]
    [OperationContract]
    public string Fs_TUYEN_SINH_TTCHUNG()
    {
        try
        {
            DataTable b_dt1 = c_ht.Fdt_BOMON_LKE();
            DataTable b_dt2 = c_nhapdiem.Fdt_KHOI_THI_LKE_DROP();
            DataTable b_dt3 = c_ht.Fdt_DAN_TOC_LKE();
            DataTable b_dt4 = c_nhapdiem.Fdt_DOI_TUONG_LKE();
            DataTable b_dt5 = c_nhapdiem.Fdt_KHU_VUC_LKE();
            DataTable b_dt6 = c_ht.Fdt_TON_GIAO_LKE();
            //Hình thức đào tạo
            DataTable b_dt_he = LIB.Data.SqlData.GetData("PHinhThuc_DT_LKE");

            DataTable b_dt_nam = c_ht.Fdt_NAM_HOC();

            return LibTable.FormTableToJson(b_dt1) + "@" + LibTable.FormTableToJson(b_dt2) + "@" + LibTable.FormTableToJson(b_dt3)
                + "@" + LibTable.FormTableToJson(b_dt4) + "@" + LibTable.FormTableToJson(b_dt5) + "@" + LibTable.FormTableToJson(b_dt6) + "@" + LibTable.FormTableToJson(b_dt_nam);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_TUYEN_SINH_NH(string id_ph, string b_nganh, string anh, object[] a_ten, object[] a_tso, object[] qt_dt)
    {
        try
        {
            DataTable b_dt = new DataTable(), b_dt_tso = new DataTable();
            b_dt_tso = LibBang.Fdt_TAO_BANG(LibConvert.Fa_OBJ_STR(a_ten), LibConvert.Fa_OBJ_STR(a_tso));
            DataRow b_r = b_dt_tso.Rows[0];
            LibTable.AddNewCol(ref b_dt, new string[] { "lop", "phieu", "ma", "ten" });
            for (int i = 0; i < qt_dt.Length; i++)
            {
                string[] gt = new string[4];
                gt[0] = ((object[])qt_dt[i])[0].ToString();
                gt[1] = "";
                gt[2] = ((object[])qt_dt[i])[1].ToString();
                gt[3] = ((object[])qt_dt[i])[2].ToString();
                LibTable.AddNewRow(ref b_dt, gt);
            }
            c_nhapdiem.P_TUYEN_SINH_NH(ref  id_ph, LibConvert.toString(b_r["phieu"]), LibConvert.toString(b_r["khoithi"]),
                LibConvert.toString(b_r["nganhdt"]), b_nganh, LibConvert.toString(b_r["ten_nganh"]), LibConvert.toString(b_r["truongnv"]), LibConvert.toString(b_r["tentruong"]),
                LibConvert.toString(b_r["nganhnv"]), LibConvert.toString(b_r["tennganhnv"]), LibConvert.toString(b_r["chuyennganhnv"]), LibConvert.toString(b_r["nam_hoc"]),
                LibConvert.toString(b_r["tendem"]), LibConvert.toString(b_r["ten"]), anh,
               LibConvert.DateToNumber(b_r["ngaysinh"].ToString()), LibConvert.toString(b_r["ton_giao"]), LibConvert.toString(b_r["Noi_sinh"]), LibConvert.toString(b_r["gioitinh"]),
               LibConvert.toString(b_r["dantoc"]), LibConvert.toString(b_r["doituong"]), LibConvert.toString(b_r["cmt"]), LibConvert.toString(b_r["ma_thuongtru"]),
               LibConvert.toString(b_r["thuongtru"]), LibConvert.toString(b_r["khuvuc"]),
               LibConvert.toString(b_r["NamTN"]), LibConvert.toString(b_r["Noi_nop"]), LibConvert.toString(b_r["donvi_dk"]), LibConvert.toString(b_r["lienhe"]),
               LibConvert.toString(b_r["sdt"]), b_dt);


            return id_ph;
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }

    }
    [WebGet]
    [OperationContract]
    public string Fs_TT_CHUNG_TUYENSINH()
    {
        try
        {
            DataTable b_dt_nam = c_ht.Fdt_NAM_HOC();
            DataTable b_dt1 = c_ht.Fdt_BOMON_LKE();
            DataTable b_dt2 = c_nhapdiem.Fdt_KHOI_THI_LKE_DROP();
            return LibTable.FormTableToJson(b_dt_nam) + "@" + LibTable.FormTableToJson(b_dt1) + "@" + LibTable.FormTableToJson(b_dt2);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_SINH_VIEN_TS_TIM(string b_nam_ts, string b_nganh, string b_nganh_xt, string b_gt, string khoi)
    {
        try
        {
            DataTable b_dt = new DataTable();
            b_dt = c_nhapdiem.Fdt_TS_TIM(b_nam_ts, b_nganh, b_nganh_xt, b_gt, khoi);
            if (b_dt.Rows.Count == 0) throw new Exception("Không có số liệu");
            return LibBang.Fs_BANG_JSON(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_PHIEU_IN(object[] a_gtri)
    {
        try
        {
            DataTable b_dt = new DataTable("B1");
            LibTable.AddNewCol(ref b_dt, new string[] { "NAM", "HO_TEN", "NGAY_SINH", "DT", "DAN_TOC", "NOI_SINH", "SBD", "PHONG", "NGUYEN_QUAN" });
            for (int i = 0; i < a_gtri.Length; i++)
            {
                object[] a_mang = (object[])a_gtri[i];
                LibTable.AddNewRow(ref b_dt, new object[] { a_mang[0], a_mang[1], a_mang[2], a_mang[3], a_mang[4], a_mang[5], a_mang[6], a_mang[7], a_mang[8] });
            }
            string url = "~/Outputs/";
            DataSet ds = new DataSet();
            ds.Tables.Add(b_dt.Copy());
            ht_bc.P_XUAT_WORD("~/Templates/", ref url, "phieu_du_thi", ds);
            return url;
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_XEP_BAODANH(string nam, string so_sv, string sbd, string phong)
    {
        try
        {
            c_nhapdiem.P_SAP_SO_BD(nam, LibConvert.ToInt(so_sv), sbd, phong);
            return "";
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_SO_BD_TTCHUNG()
    {
        try
        {
            DataTable b_dt = c_ht.Fdt_NAM_HOC();
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }

    //Xết tuyển
    [OperationContract]
    public string Fs_XET_TUYEN_TTCHUNG()
    {
        try
        {
            DataTable b_dt = c_ht.Fdt_NAM_HOC();
            DataTable b_dt1 = c_ht.Fdt_BOMON_LKE();
            DataTable b_dt2 = c_nhapdiem.Fdt_KHOI_THI_LKE_DROP();
            return LibTable.FormTableToJson(b_dt) + "@" + LibTable.FormTableToJson(b_dt2) + "@" + LibTable.FormTableToJson(b_dt1);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    //Cập nhật thông tin tuyển sinh
    [OperationContract]
    public string Fs_TT_TUYEN_SINH_NH(string id_ts, string nam_ts, string nganh_ts, string khoi, string sl, string diem)
    {
        try
        {
            luu.ht_nsd _nsd = new luu.ht_nsd();
            c_nhapdiem.PTT_TUYEN_SINH_NH(ref id_ts, nam_ts, nganh_ts, khoi, LibConvert.ToInt(sl), LibConvert.ToInt(diem), _nsd.user);
            return id_ts + "@" + Fs_TT_TUYEN_SINH_TIM(nam_ts, khoi);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    //Liệt kê
    [OperationContract]
    public string Fs_TT_TUYEN_SINH_TIM(string nam, string khoi)
    {
        try
        {
            DataTable b_d = LIB.Data.SqlData.GetData(new object[] { nam, khoi }, "PTT_TUYEN_SINH_TIM");
            return LibTable.FormTableToJson(b_d);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    //xóa
    [OperationContract]
    public string Fs_TT_TUYEN_SINH_XOA(string id_ts)
    {
        try
        {
            LIB.Data.SqlData.ExeCuteProcedure(id_ts, "PTT_TUYEN_SINH_XOA");
            return "";
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    //CT
    [OperationContract]
    public string Fs_TT_TUYEN_SINH_CT(string id_ts)
    {
        try
        {
            DataTable b_d = LIB.Data.SqlData.GetData(id_ts, "PTT_TUYEN_SINH_CT");
            string id_nganhhoc = string.Empty;
            if (b_d.Rows.Count > 0)
                id_nganhhoc = LibConvert.toString(b_d.Rows[0]["bo_mon"]);
            DataTable b_dt1 = c_ht.Fdt_NGANHDT_NGANHHOC_LKE(id_nganhhoc);
            return LibTable.FormTableToJson(b_d) + "@" + LibTable.FormTableToJson(b_dt1);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    //Danh sách phòng thi
    [OperationContract]
    public string Fs_TT_TUYEN_SINH_PHONG(string nam)
    {
        try
        {
            DataTable b_dt = LIB.Data.SqlData.GetData(nam, "PTT_TUYEN_SINH_PHONG");
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    //danh sách phòng thi
    [OperationContract]
    public string Fs_TUYEN_SINH_DS_THI(string nam, string khoi, string phong)
    {
        try
        {
            DataTable b_dt = LIB.Data.SqlData.GetData(new object[] { nam, khoi, phong }, "PTUYEN_SINH_DS_THI");
            string url = "~/Outputs/";
            DataSet ds = new DataSet();

            DataTable b_dt1 = new DataTable("B");
            LibTable.AddNewCol(ref b_dt1, new string[] { "PHONG", "KHOI" });
            LibTable.AddNewRow(ref b_dt1, new object[] { phong, khoi });
            b_dt.TableName = "B1";

            ds.Tables.Add(b_dt.Copy());
            ds.Tables.Add(b_dt1.Copy());

            ht_bc.P_XUAT_WORD("~/Templates/", ref url, "ds_du_thi_ts", ds);
            return url;
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }

    //Điểm tuyển sinh
    [OperationContract]
    public string Fs_DIEM_TUYEN_SINH_TTCHUNG()
    {
        try
        {
            DataTable b_dt = c_ht.Fdt_NAM_HOC();
            //Khối
            DataTable b_dt1 = c_nhapdiem.Fdt_KHOI_THI_LKE_DROP();
            //Loại điểm
            return LibTable.FormTableToJson(b_dt) + "@" + LibTable.FormTableToJson(b_dt1);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_DIEM_TUYEN_SINH_PH(string nam_ts, string khoi_thi)
    {
        try
        {
            DataTable b_dt = LIB.Data.SqlData.GetData(nam_ts, "PTT_TUYEN_SINH_PHONG");
            DataTable b_dt1 = LIB.Data.SqlData.GetData(khoi_thi, "P_DIEM_TUYEN_SINH_LKE_MON");
            return LibTable.FormTableToJson(b_dt) + "@" + LibTable.FormTableToJson(b_dt1);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_TUYEN_SINH_DIEM_TIM(string nam_ts, string khoi_thi, string phong, string b_monid)
    {
        try
        {
            DataTable b_dt = LIB.Data.SqlData.GetData(new object[] { nam_ts, khoi_thi, phong, b_monid }, "P_DIEM_TUYEN_SINH_TIM");
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    //Nhập điểm
    [OperationContract]
    public string Fs_TUYEN_SINH_DIEM_NH(string id_mon, object[] a_diem)
    {
        try
        {
            DataTable b_dt = new DataTable();
            LibTable.AddNewCol(ref b_dt, new string[] { "id_diem", "id_ts", "id_mon", "diem", "nsd" });
            luu.ht_nsd _nsd = new luu.ht_nsd();
            for (int i = 0; i < a_diem.Length; i++)
            {
                object[] a_gtri = new object[b_dt.Columns.Count];
                a_gtri[0] = ((object[])a_diem[i])[0];
                a_gtri[1] = ((object[])a_diem[i])[1];
                a_gtri[2] = id_mon;
                a_gtri[3] = ((object[])a_diem[i])[2];
                a_gtri[4] = _nsd.user;

                LibTable.AddNewRow(ref b_dt, a_gtri);
            }
            LIB.Data.SqlData.ExeCuteProcedure(b_dt, "PTUYEN_SINH_DIEM_NH");
            return "";
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    //Phiếu báo điểm
    [OperationContract]
    public string Fs_TUYEN_SINH_DIEM_PBD(string nam_ts, string khoi_thi)
    {
        try
        {
            DataTable b_dt = LIB.Data.SqlData.GetData(new object[] { nam_ts, khoi_thi }, "PTUYEN_SINH_BAO_DIEM");
            string url = "~/Outputs/";
            DataSet ds = new DataSet();
            DataTable b_dt1 = new DataTable("B");
            LibTable.AddNewCol(ref b_dt1, new string[] { "PHONG", "KHOI" });
            LibTable.AddNewRow(ref b_dt1, new object[] { nam_ts, khoi_thi });
            b_dt.TableName = "B1";

            ds.Tables.Add(b_dt.Copy());
            ds.Tables.Add(b_dt1.Copy());
            ht_bc.P_XUAT_WORD("~/Templates/", ref url, "phieu_bao_diem", ds);
            return url;
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    //Phiếu báo điểm
    [OperationContract]
    public string Fs_TUYEN_SINH_DIEM_HOC(string nam_ts, string khoi_thi, string nganhID)
    {
        try
        {
            DataTable b_dt = LIB.Data.SqlData.GetData(new object[] { nam_ts, khoi_thi }, "PTUYEN_SINH_BAO_NHOC");
            string url = "~/Outputs/";
            DataSet ds = new DataSet();
            DataTable b_dt1 = new DataTable("B");
            LibTable.AddNewCol(ref b_dt1, new string[] { "PHONG", "KHOI" });
            LibTable.AddNewRow(ref b_dt1, new object[] { nam_ts, khoi_thi });
            b_dt.TableName = "B1";

            ds.Tables.Add(b_dt.Copy());
            ds.Tables.Add(b_dt1.Copy());
            ht_bc.P_XUAT_WORD("~/Templates/", ref url, "phieu_bao_diem_do", ds);
            return url;
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    #region Ma_SV

    [OperationContract]
    public string Fs_MA_SV_LKE(string nam_ts)
    {
        try
        {
            DataTable b_dt = LIB.Data.SqlData.GetData(nam_ts, "PMA_SV_LKE");
            return (b_dt == null || b_dt.Rows.Count == 0 ? "" : LibConvert.toString(b_dt.Rows[0]["ma_sv"]));
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_MA_SV_NH(string nam_ts, string b_ma_sv)
    {
        try
        {
            LIB.Data.SqlData.ExeCuteProcedure(new object[] { nam_ts, b_ma_sv }, "PMA_SV_NH");
            return "";
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }

    //Nhận vớt
    [OperationContract]
    public string Fs_TUYEN_SNH_SV_VOT(string nam_ts, string b_khoi, string b_nganh, string so_sv)
    {
        try
        {
            DataTable b_dt = new DataTable();
            b_dt = LIB.Data.SqlData.GetData(new object[] { nam_ts, b_khoi, b_nganh, so_sv }, "PTUYEN_SINH_XET_TUYEN");
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }

    [OperationContract]
    public string Fs_TUYEN_SINH_DIEM_VOT_NH(string[] id_ts)
    {
        try
        {
            DataTable b_dt = new DataTable();
            LibTable.AddNewCol(ref b_dt, "id_ts");
            for (int i = 0; i < id_ts.Length; i++)
            {
                LibTable.AddNewRow(ref b_dt, id_ts[i]);
            }
            LIB.Data.SqlData.ExeCuteProcedure(b_dt, "PTUYEN_SINH_DIEM_VOT_NH");
            return "";
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    //ếp lớp
    [OperationContract]
    public string Fs_XEP_LOP_TTCHUNG()
    {
        try
        {
            DataTable b_dt = c_ht.Fdt_NAM_HOC();
            DataTable b_dt1 = c_ht.Fdt_BOMON_LKE();
            DataTable b_dt2 = c_nhapdiem.Fdt_KHOI_THI_LKE_DROP();
            DataTable b_dt3 = LIB.Data.SqlData.GetData("PLOP_HOC_CHUA_PHAN");

            return LibTable.FormTableToJson(b_dt) + "@" + LibTable.FormTableToJson(b_dt2) + "@" + LibTable.FormTableToJson(b_dt1) + "@" + LibTable.FormTableToJson(b_dt3);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_XEP_LOP_TIM(string id_nam, string khoi, string nganh)
    {
        try
        {
            DataTable b_dt = new DataTable();
            b_dt = LIB.Data.SqlData.GetData(new object[] { id_nam, khoi, nganh }, "PPHAN_LOP_TIM");
            return LibTable.FormTableToJson(b_dt);
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }
    [OperationContract]
    public string Fs_XEP_LOP_NH(string[] id_ts, string lop, string b_nam_hoc)
    {
        try
        {
            DataTable b_dt = new DataTable();
            LibTable.AddNewCol(ref b_dt, new string[] { "id_ts", "lop", "nam_ts" });
            for (int i = 0; i < id_ts.Length; i++)
            {
                LibTable.AddNewRow(ref b_dt, new object[] { id_ts[i], lop, b_nam_hoc });
            }
            LIB.Data.SqlData.ExeCuteProcedure(b_dt, "PXEP_LOP_NH");
            return "";
        }
        catch (Exception ex)
        {
            return "loi:" + ex.Message + ":loi";
        }
    }

    #endregion
}
